package com.bluestone.context;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import com.bluestone.scripts.Execute;
import com.bluestone.scripts.Input;
import com.bluestone.scripts.Project;
import com.bluestone.scripts.Property;
import com.bluestone.scripts.Resource;
import com.bluestone.scripts.TestSuite;
import com.bluestone.util.Util;

public class ProjectContext extends ContextBase {

	private static ProjectContext context = null;
	private ArrayList<ExecuteContext> executeContexts = new ArrayList<ExecuteContext>();
	private ConfigContext configContext = null;
	private Project project = null;
	private static String file = "./scripts/luster.xml";	

	public Project getProject() {
		return project;
	}
	
	public static ProjectContext getInstance(String f){
		if(context==null){
			if(f!=null && f.length()>0){
				file = f;
			}
			context = new ProjectContext();
			context.configContext = ConfigContext.getInstance();
			context.loadProjectFile(file);
			context.setAllowFailureCount(context.getProject().getFailurecount());
			context.setAllowRetryCount(context.getProject().getRetrycount());
			context.createExecuteContext();
		}	

		return context;		
	}
	
	private void createExecuteContext(){
		List executes = project.getExecutes();
		for(int i=0; i<executes.size(); i++){
			Execute execute = (Execute) executes.get(i);
			TestSuite testSuite = project.getTestSuite(execute);
			ExecuteContext context = new ExecuteContext(execute);
			executeContexts.add(context);						
		}
	}

	public ExecuteContext getExecuteContext(Execute execute){
		ExecuteContext context = null;
		for(int i=0; i< executeContexts.size(); i++){
			ExecuteContext temp = (ExecuteContext)executeContexts.get(i);
			if(execute.getId().equalsIgnoreCase(temp.getId())){
				context = temp;
				break;
			}
		}
		return context;
		
	}
	private void loadProjectFile(String f){
		if(f!=null && f.length()>0){
			this.file = f;
		}
		try {
			JAXBContext context = JAXBContext.newInstance(Project.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			project = (Project) unmarshaller.unmarshal(new File(file));
			addExecuteLog("load project file:" + file);
		} catch (Exception me) {
			addExecuteLog("load project file failure:" + file);
			Util.error("load project file failure:" + file);
		}
		if (project != null) {
			initResources(project);
			initInputs(project);
			initProperty(project);

		}			
	}
	
	private void initResources(Project project) {
		ArrayList list = (ArrayList) project.getResources();
		if(list==null) return;
		for (int i = 0; i < list.size(); i++) {
			Resource resource = (Resource) list.get(i);
			resource.loadObject(this);
		}
	}
	
	private void initInputs(Project project) {
		ArrayList list = (ArrayList) project.getInputs();
		for (int i = 0; i < list.size(); i++) {
			Input input = (Input) list.get(i);
			input.loadObject(this);
		}
	}
	
	private void initProperty(Project project) {
		ArrayList list = (ArrayList) project.getProperties();
		if(list==null) return;
		for (int i = 0; i < list.size(); i++) {
			Property property = (Property) list.get(i);
			property.loadObject(this);
		}
	}

	public ConfigContext getConfigContext() {
		return configContext;
	}
	
	public String getAllowFailureCount(){
		return (String)map.get(ALLOWFAILURECOUNT);
	}	
	
	public void setAllowFailureCount(String count){
		map.put(ALLOWFAILURECOUNT,count);
	}
	
	public String getAllowRetryCount(){
		return (String)map.get(ALLOWRETRYCOUNT);
	}	
	
	public void setAllowRetryCount(String count){
		map.put(ALLOWRETRYCOUNT,count);
	}


}
